<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta name="google-site-verification" content="yF8HXsFuCTrePDFRnSkN2kkzV2ypBjtywzXsfVXOOV8">
  <meta name="baidu-site-verification" content="VxrVAiQAZ9">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yang0033.gitee.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":true,"preload":true},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="这段时间阅读ibireme神的源码，看到了 CoreText 排版这一块，里面包含了很多文字排版的专有名词，这里做一下整理，顺便帮大家安利一下。 CoreText 框架中最常用的几个类：   CTFont CTFontCollection CTFontDescriptor CTFrame CTFramesetter CTGlyphInfo CTLine CTParagraphStyle CTRun">
<meta property="og:type" content="article">
<meta property="og:title" content="ReactiveCocoa 设计规范">
<meta property="og:url" content="http://yang0033.gitee.io/2016/01/27/CoreText%E5%9F%BA%E7%A1%80%E6%A6%82%E5%BF%B5%EF%BC%88%E6%89%AB%E7%9B%B2%E7%AF%87%EF%BC%89/index.html">
<meta property="og:site_name" content="SuperYang&#96;s Blog">
<meta property="og:description" content="这段时间阅读ibireme神的源码，看到了 CoreText 排版这一块，里面包含了很多文字排版的专有名词，这里做一下整理，顺便帮大家安利一下。 CoreText 框架中最常用的几个类：   CTFont CTFontCollection CTFontDescriptor CTFrame CTFramesetter CTGlyphInfo CTLine CTParagraphStyle CTRun">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/CoreText%E6%9E%B6%E6%9E%84%E5%9B%BE.png?raw=true">
<meta property="og:image" content="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/%E6%96%87%E5%AD%97%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%860.gif?raw=true">
<meta property="og:image" content="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/%E6%96%87%E5%AD%97%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%861.gif?raw=true">
<meta property="og:image" content="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/ex0.jpg?raw=true">
<meta property="og:image" content="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/ex1.jpg?raw=true">
<meta property="og:image" content="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/ex2.jpg?raw=true">
<meta property="og:image" content="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/ex3.jpg?raw=true">
<meta property="og:image" content="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/ex4.jpg?raw=true">
<meta property="og:image" content="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/ex5.jpg?raw=true">
<meta property="og:image" content="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/ex6.jpg?raw=true">
<meta property="og:image" content="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/ex7.jpg?raw=true">
<meta property="og:image" content="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/ex8.jpg?raw=true">
<meta property="og:image" content="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/ex9.jpg?raw=true">
<meta property="article:published_time" content="2016-01-27T12:16:34.000Z">
<meta property="article:modified_time" content="2020-07-09T08:00:05.251Z">
<meta property="article:author" content="SuperYang">
<meta property="article:tag" content="RAC">
<meta property="article:tag" content="FRP">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/CoreText%E6%9E%B6%E6%9E%84%E5%9B%BE.png?raw=true">

<link rel="canonical" href="http://yang0033.gitee.io/2016/01/27/CoreText%E5%9F%BA%E7%A1%80%E6%A6%82%E5%BF%B5%EF%BC%88%E6%89%AB%E7%9B%B2%E7%AF%87%EF%BC%89/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>ReactiveCocoa 设计规范 | SuperYang`s Blog</title>
  
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-71812998-3"></script>
    <script>
      if (CONFIG.hostname === location.hostname) {
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());
        gtag('config', 'UA-71812998-3');
      }
    </script>


  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?8ff3295288514de6f89f314c23ee40ab";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">SuperYang`s Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">落魄程序员在线炒饭</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-message">

    <a href="/message/" rel="section"><i class="fa fa-music fa-fw"></i>留言板 | Music</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yang0033.gitee.io/2016/01/27/CoreText%E5%9F%BA%E7%A1%80%E6%A6%82%E5%BF%B5%EF%BC%88%E6%89%AB%E7%9B%B2%E7%AF%87%EF%BC%89/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://tva2.sinaimg.cn/crop.259.17.290.290.180/c2abdfa9jw8ez7appr3p2j20g40a2t91.jpg?KID=imgbed,tva&Expires=1594186820&ssig=BdxhDdi1Ti">
      <meta itemprop="name" content="SuperYang">
      <meta itemprop="description" content="牛肉炒饭不要香菜，老板收钱">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="SuperYang`s Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          ReactiveCocoa 设计规范
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2016-01-27 20:16:34" itemprop="dateCreated datePublished" datetime="2016-01-27T20:16:34+08:00">2016-01-27</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-07-09 16:00:05" itemprop="dateModified" datetime="2020-07-09T16:00:05+08:00">2020-07-09</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/iOS/" itemprop="url" rel="index"><span itemprop="name">iOS</span></a>
                </span>
            </span>

          
            <span id="/2016/01/27/CoreText%E5%9F%BA%E7%A1%80%E6%A6%82%E5%BF%B5%EF%BC%88%E6%89%AB%E7%9B%B2%E7%AF%87%EF%BC%89/" class="post-meta-item leancloud_visitors" data-flag-title="ReactiveCocoa 设计规范" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2016/01/27/CoreText%E5%9F%BA%E7%A1%80%E6%A6%82%E5%BF%B5%EF%BC%88%E6%89%AB%E7%9B%B2%E7%AF%87%EF%BC%89/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2016/01/27/CoreText%E5%9F%BA%E7%A1%80%E6%A6%82%E5%BF%B5%EF%BC%88%E6%89%AB%E7%9B%B2%E7%AF%87%EF%BC%89/" itemprop="commentCount"></span>
    </a>
  </span>
  
  <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>9.8k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>9 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>这段时间阅读ibireme神的源码，看到了 CoreText 排版这一块，里面包含了很多文字排版的专有名词，这里做一下整理，顺便帮大家安利一下。</p>
<p>CoreText 框架中最常用的几个类： </p>
<ul>
<li><a href="https://developer.apple.com/library/mac/documentation/Carbon/Reference/CTFontRef/Reference/reference.html#//apple_ref/doc/uid/TP40005110" target="_blank" rel="noopener">CTFont</a></li>
<li><a href="https://developer.apple.com/library/mac/documentation/Carbon/Reference/CTFontCollectionRef/Reference/reference.html#//apple_ref/doc/uid/TP40005104" target="_blank" rel="noopener">CTFontCollection</a></li>
<li><a href="https://developer.apple.com/library/mac/documentation/Carbon/Reference/CTFontDescriptorRef/Reference/reference.html#//apple_ref/doc/uid/TP40005107" target="_blank" rel="noopener">CTFontDescriptor</a></li>
<li><a href="https://developer.apple.com/library/mac/documentation/Carbon/Reference/CTFrameRef/Reference/reference.html#//apple_ref/doc/uid/TP40005113" target="_blank" rel="noopener">CTFrame</a></li>
<li><a href="https://developer.apple.com/library/mac/documentation/Carbon/Reference/CTFramesetterRef/Reference/reference.html#//apple_ref/doc/uid/TP40005105" target="_blank" rel="noopener">CTFramesetter</a></li>
<li><a href="https://developer.apple.com/library/mac/documentation/Carbon/Reference/CTGlyphInfoRef/Reference/reference.html#//apple_ref/doc/uid/TP40005108" target="_blank" rel="noopener">CTGlyphInfo</a></li>
<li><a href="https://developer.apple.com/library/mac/documentation/Carbon/Reference/CTLineRef/Reference/reference.html#//apple_ref/doc/uid/TP40005111" target="_blank" rel="noopener">CTLine</a></li>
<li><a href="https://developer.apple.com/library/mac/documentation/Carbon/Reference/CTParagraphStyleRef/Reference/reference.html#//apple_ref/doc/uid/TP40005114" target="_blank" rel="noopener">CTParagraphStyle</a></li>
<li><a href="https://developer.apple.com/library/mac/documentation/Carbon/Reference/CTRunRef/Reference/reference.html#//apple_ref/doc/uid/TP40005106" target="_blank" rel="noopener">CTRun</a></li>
<li><a href="https://developer.apple.com/library/mac/documentation/Carbon/Reference/CTTextTabRef/Reference/reference.html#//apple_ref/doc/uid/TP40005109" target="_blank" rel="noopener">CTTextTab</a></li>
<li><a href="https://developer.apple.com/library/mac/documentation/Carbon/Reference/CTTypesetterRef/Reference/reference.html#//apple_ref/doc/uid/TP40005112" target="_blank" rel="noopener">CTTypesett</a></li>
</ul>
<p>下面是该框架的结构图</p>
<p><img src="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/CoreText%E6%9E%B6%E6%9E%84%E5%9B%BE.png?raw=true" alt="coretext架构图"></p>
<a id="more"></a> 
<p>CTFrame 作为一个整体的画布(Canvas)，其中由行(CTLine)组成，而每行可以分为一个或多个小方块（CTRun）。<br>注意：你不需要自己创建CTRun，Core Text将根据NSAttributedString的属性来自动创建CTRun。每个CTRun对象对应不同的属性，正因此，你可以自由的控制字体、颜色、字间距等等信息。<br>通常处理步聚：<br>1.使用core text就是先有一个要显示的string，然后定义这个string每个部分的样式－&gt;attributedString －&gt; 生成 CTFramesetter -&gt; 得到CTFrame -&gt; 绘制（CTFrameDraw）<br>其中可以更详细的设置换行方式，对齐方式，绘制区域的大小等。<br>2.绘制只是显示，点击事件就需要一个判断了。<br>CTFrame 包含了多个CTLine,并且可以得到各个line的其实位置与大小。判断点击处在不在某个line上。CTLine 又可以判断这个点(相对于ctline的坐标)处的文字范围。然后遍历这个string的所有NSTextCheckingResult，根据result的rang判断点击处在不在这个rang上，从而得到点击的链接与位置。</p>
<h3 id="字体的基本知识"><a href="#字体的基本知识" class="headerlink" title="字体的基本知识"></a>字体的基本知识</h3><p><strong>字体(Font):</strong>是一系列字号、样式和磅值相同的字符(例如:10磅黑体Palatino)。现多被视为字样的同义词</p>
<p><strong>字面(Face):</strong>是所有字号的磅值和格式的综合</p>
<p><strong>字体集(Font family):</strong>是一组相关字体(例如:Franklin family包括Franklin Gothic、Fran-klinHeavy和Franklin Compressed)</p>
<p><strong>磅值(Weight):</strong>用于描述字体粗度。典型的磅值,从最粗到最细,有极细、细、book、中等、半粗、粗、较粗、极粗</p>
<p><strong>样式(Style):</strong>字形有三种形式:Roman type是直体;oblique type是斜体;utakuc type是斜体兼曲线(比Roman type更像书法体)。</p>
<p><strong>x高度(X height):</strong>指小写字母的平均高度(以x为基准)。磅值相同的两字母,x高度越大的字母看起来比x高度小的字母要大</p>
<p><strong>Cap高度(Cap height):</strong>与x高度相似。指大写字母的平均高度(以C为基准)</p>
<p><strong>下行字母(Descender):</strong>例如在字母q中,基线以下的字母部分叫下伸部分</p>
<p><strong>上行字母(Ascender):</strong>x高度以上的部分(比如字母b)叫做上伸部分</p>
<p><strong>基线(Baseline):</strong>通常在x、v、b、m下的那条线<br>描边(Stroke):组成字符的线或曲线。可以加粗或改变字符形状</p>
<p><strong>衬线(Serif):</strong>用来使字符更可视的一条水平线。如字母左上角和下部的水平线。</p>
<p><strong>无衬线(Sans Serif):</strong>可以让排字员不使用衬线装饰。</p>
<p><strong>方形字(Block):</strong>这种字体的笔画使字符看起来比无衬线字更显眼,但还不到常见的衬线字的程度。例如Lubalin Graph就是方形字,这种字看起来好像是木头块刻的一样</p>
<p><strong>手写体脚本(Calligraphic script):</strong>是一种仿效手写体的字体。例如Murray Hill或者Fraktur字体</p>
<p><strong>艺术字(Decorative):</strong>像绘画般的字体</p>
<p><strong>Pi符号(Pisymbol):</strong>非标准的字母数字字符的特殊符号。例如Wingdings和Mathematical Pi</p>
<p><strong>连写(Ligature):</strong>是一系列连写字母如fi、fl、ffi或ffl。由于字些字母形状的原因经常被连写,故排字员已习惯将它们连写。</p>
<p>读完了上面这些概念，可以参考一下下面的图片，看看具体的位置</p>
<p><img src="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/%E6%96%87%E5%AD%97%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%860.gif?raw=true" alt="文字结构"><br><img src="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/%E6%96%87%E5%AD%97%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%861.gif?raw=true" alt="文字结构"></p>
<p>其中，在 Apple 的 SDK 中是这样定义这些属性的</p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="built_in">CFStringRef</span> kCTCharacterShapeAttributeName;              </span><br><span class="line"><span class="comment">//字体形状属性  必须是CFNumberRef对象默认为0，非0则对应相应的字符形状定义，如1表示传统字符形状</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="built_in">CFStringRef</span> kCTFontAttributeName;                        </span><br><span class="line"><span class="comment">//字体属性   必须是CTFont对象</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="built_in">CFStringRef</span> kCTKernAttributeName;                        </span><br><span class="line"><span class="comment">//字符间隔属性 必须是CFNumberRef对象</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="built_in">CFStringRef</span> kCTLigatureAttributeName;                 </span><br><span class="line"><span class="comment">//设置是否使用连字属性，设置为0，表示不使用连字属性。标准的英文连字有FI,FL.默认值为1，既是使用标准连字。也就是当搜索到f时候，会把fl当成一个文字。必须是CFNumberRef 默认为1,可取0,1,2</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="built_in">CFStringRef</span> kCTForegroundColorAttributeName;             </span><br><span class="line"><span class="comment">//字体颜色属性  必须是CGColor对象，默认为black</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="built_in">CFStringRef</span> kCTForegroundColorFromContextAttributeName; </span><br><span class="line"> <span class="comment">//上下文的字体颜色属性 必须为CFBooleanRef 默认为False</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="built_in">CFStringRef</span> kCTParagraphStyleAttributeName;              </span><br><span class="line"><span class="comment">//段落样式属性 必须是CTParagraphStyle对象 默认为NIL</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="built_in">CFStringRef</span> kCTStrokeWidthAttributeName;              </span><br><span class="line"><span class="comment">//笔画线条宽度 必须是CFNumberRef对象，默为0.0f，标准为3.0f</span></span><br><span class="line"><span class="keyword">const</span> <span class="built_in">CFStringRef</span> kCTStrokeColorAttributeName;              </span><br><span class="line"><span class="comment">//笔画的颜色属性 必须是CGColorRef 对象，默认为前景色</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="built_in">CFStringRef</span> kCTSuperscriptAttributeName;              </span><br><span class="line"><span class="comment">//设置字体的上下标属性 必须是CFNumberRef对象 默认为0,可为-1为下标,1为上标，需要字体支持才行。如排列组合的样式Cn1</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="built_in">CFStringRef</span> kCTUnderlineColorAttributeName;           </span><br><span class="line"><span class="comment">//字体下划线颜色属性 必须是CGColorRef对象，默认为前景色</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="built_in">CFStringRef</span> kCTUnderlineStyleAttributeName;           </span><br><span class="line"><span class="comment">//字体下划线样式属性 必须是CFNumberRef对象,默为kCTUnderlineStyleNone 可以通过CTUnderlineStypleModifiers 进行修改下划线风格</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="built_in">CFStringRef</span> kCTVerticalFormsAttributeName;</span><br><span class="line"><span class="comment">//文字的字形方向属性 必须是CFBooleanRef 默认为false，false表示水平方向，true表示竖直方向</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="built_in">CFStringRef</span> kCTGlyphInfoAttributeName;</span><br><span class="line"><span class="comment">//字体信息属性 必须是CTGlyphInfo对象</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="built_in">CFStringRef</span> kCTRunDelegateAttributeName</span><br><span class="line"><span class="comment">//CTRun 委托属性 必须是CTRunDelegate对象</span></span><br></pre></td></tr></table></figure>

<p>例如：</p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">NSMutableAttributedString</span> *mabstring = [[<span class="built_in">NSMutableAttributedString</span> alloc]initWithString:<span class="string">@"This is a test of characterAttribute. 中文字符"</span>];</span><br></pre></td></tr></table></figure>
<p><img src="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/ex0.jpg?raw=true" alt="文字结构"></p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//设置字体属性</span></span><br><span class="line">  <span class="built_in">CTFontRef</span> font = <span class="built_in">CTFontCreateWithName</span>(<span class="built_in">CFSTR</span>(<span class="string">"Georgia"</span>), <span class="number">40</span>, <span class="literal">NULL</span>);</span><br><span class="line">  [mabstring addAttribute:(<span class="keyword">id</span>)kCTFontAttributeName value:(<span class="keyword">id</span>)font range:<span class="built_in">NSMakeRange</span>(<span class="number">0</span>, <span class="number">4</span>)];</span><br></pre></td></tr></table></figure>
<p><img src="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/ex1.jpg?raw=true" alt="文字结构"></p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//设置斜体字</span></span><br><span class="line">    <span class="built_in">CTFontRef</span> font = <span class="built_in">CTFontCreateWithName</span>((<span class="built_in">CFStringRef</span>)[<span class="built_in">UIFont</span> italicSystemFontOfSize:<span class="number">20</span>].fontName, <span class="number">14</span>, <span class="literal">NULL</span>);</span><br><span class="line">    [mabstring addAttribute:(<span class="keyword">id</span>)kCTFontAttributeName value:(<span class="keyword">id</span>)font range:<span class="built_in">NSMakeRange</span>(<span class="number">0</span>, <span class="number">4</span>)];</span><br></pre></td></tr></table></figure>

<p><img src="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/ex2.jpg?raw=true" alt="文字结构"></p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//下划线</span></span><br><span class="line">    [mabstring addAttribute:(<span class="keyword">id</span>)kCTUnderlineStyleAttributeName value:(<span class="keyword">id</span>)[<span class="built_in">NSNumber</span> numberWithInt:kCTUnderlineStyleDouble] range:<span class="built_in">NSMakeRange</span>(<span class="number">0</span>, <span class="number">4</span>)];</span><br></pre></td></tr></table></figure>
<p><img src="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/ex3.jpg?raw=true" alt="文字结构"></p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//下划线颜色</span></span><br><span class="line">    [mabstring addAttribute:(<span class="keyword">id</span>)kCTUnderlineColorAttributeName value:(<span class="keyword">id</span>)[<span class="built_in">UIColor</span> redColor].CGColor range:<span class="built_in">NSMakeRange</span>(<span class="number">0</span>, <span class="number">4</span>)];</span><br></pre></td></tr></table></figure>
<p><img src="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/ex4.jpg?raw=true" alt="文字结构"></p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//设置字体简隔 eg:test </span></span><br><span class="line">    <span class="keyword">long</span> number = <span class="number">10</span>;</span><br><span class="line">    <span class="built_in">CFNumberRef</span> num = <span class="built_in">CFNumberCreate</span>(kCFAllocatorDefault,kCFNumberSInt8Type,&amp;number);</span><br><span class="line">    [mabstring addAttribute:(<span class="keyword">id</span>)kCTKernAttributeName value:(<span class="keyword">id</span>)num range:<span class="built_in">NSMakeRange</span>(<span class="number">10</span>, <span class="number">4</span>)];</span><br></pre></td></tr></table></figure>
<p><img src="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/ex5.jpg?raw=true" alt="文字结构"></p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//设置连字</span></span><br><span class="line"><span class="keyword">long</span> number = <span class="number">1</span>;</span><br><span class="line">    <span class="built_in">CFNumberRef</span> num = <span class="built_in">CFNumberCreate</span>(kCFAllocatorDefault,kCFNumberSInt8Type,&amp;number);</span><br><span class="line">    [mabstring addAttribute:(<span class="keyword">id</span>)kCTLigatureAttributeName value:(<span class="keyword">id</span>)num range:<span class="built_in">NSMakeRange</span>(<span class="number">0</span>, [str length])];</span><br></pre></td></tr></table></figure>
<p>暂时没有效果图</p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//设置字体颜色</span></span><br><span class="line">    [mabstring addAttribute:(<span class="keyword">id</span>)kCTForegroundColorAttributeName value:(<span class="keyword">id</span>)[<span class="built_in">UIColor</span> redColor].CGColor range:<span class="built_in">NSMakeRange</span>(<span class="number">0</span>, <span class="number">9</span>)];</span><br></pre></td></tr></table></figure>
<p><img src="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/ex6.jpg?raw=true" alt="文字结构"></p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//设置字体颜色为前影色</span></span><br><span class="line">    <span class="built_in">CFBooleanRef</span> flag = kCFBooleanTrue;</span><br><span class="line">    [mabstring addAttribute:(<span class="keyword">id</span>)kCTForegroundColorFromContextAttributeName value:(<span class="keyword">id</span>)flag range:<span class="built_in">NSMakeRange</span>(<span class="number">5</span>, <span class="number">10</span>)];</span><br></pre></td></tr></table></figure>
<p>暂无效果。。</p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//设置空心字</span></span><br><span class="line">    <span class="keyword">long</span> number = <span class="number">2</span>;</span><br><span class="line">    <span class="built_in">CFNumberRef</span> num = <span class="built_in">CFNumberCreate</span>(kCFAllocatorDefault,kCFNumberSInt8Type,&amp;number);</span><br><span class="line">    [mabstring addAttribute:(<span class="keyword">id</span>)kCTStrokeWidthAttributeName value:(<span class="keyword">id</span>)num range:<span class="built_in">NSMakeRange</span>(<span class="number">0</span>, [str length])];</span><br></pre></td></tr></table></figure>
<p><img src="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/ex7.jpg?raw=true" alt="文字结构"></p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//设置空心字</span></span><br><span class="line">    <span class="keyword">long</span> number = <span class="number">2</span>;</span><br><span class="line">    <span class="built_in">CFNumberRef</span> num = <span class="built_in">CFNumberCreate</span>(kCFAllocatorDefault,kCFNumberSInt8Type,&amp;number);</span><br><span class="line">    [mabstring addAttribute:(<span class="keyword">id</span>)kCTStrokeWidthAttributeName value:(<span class="keyword">id</span>)num range:<span class="built_in">NSMakeRange</span>(<span class="number">0</span>, [str length])];</span><br><span class="line">     </span><br><span class="line">    <span class="comment">//设置空心字颜色</span></span><br><span class="line">    [mabstring addAttribute:(<span class="keyword">id</span>)kCTStrokeColorAttributeName value:(<span class="keyword">id</span>)[<span class="built_in">UIColor</span> greenColor].CGColor range:<span class="built_in">NSMakeRange</span>(<span class="number">0</span>, [str length])];</span><br></pre></td></tr></table></figure>

<p><img src="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/ex8.jpg?raw=true" alt="文字结构"></p>
<p><strong>在设置空心字颜色时，必须先将字体高为空心，否则设置颜色是没有效果的。</strong></p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">//对同一段字体进行多属性设置    </span></span><br><span class="line">    <span class="comment">//红色</span></span><br><span class="line">    <span class="built_in">NSMutableDictionary</span> *attributes = [<span class="built_in">NSMutableDictionary</span> dictionaryWithObject:(<span class="keyword">id</span>)[<span class="built_in">UIColor</span> redColor].CGColor forKey:(<span class="keyword">id</span>)kCTForegroundColorAttributeName];</span><br><span class="line">    <span class="comment">//斜体</span></span><br><span class="line">    <span class="built_in">CTFontRef</span> font = <span class="built_in">CTFontCreateWithName</span>((<span class="built_in">CFStringRef</span>)[<span class="built_in">UIFont</span> italicSystemFontOfSize:<span class="number">20</span>].fontName, <span class="number">40</span>, <span class="literal">NULL</span>);</span><br><span class="line">    [attributes setObject:(<span class="keyword">id</span>)font forKey:(<span class="keyword">id</span>)kCTFontAttributeName];</span><br><span class="line">    <span class="comment">//下划线</span></span><br><span class="line">    [attributes setObject:(<span class="keyword">id</span>)[<span class="built_in">NSNumber</span> numberWithInt:kCTUnderlineStyleDouble] forKey:(<span class="keyword">id</span>)kCTUnderlineStyleAttributeName];</span><br><span class="line">    </span><br><span class="line">    [mabstring addAttributes:attributes range:<span class="built_in">NSMakeRange</span>(<span class="number">0</span>, <span class="number">4</span>)];</span><br></pre></td></tr></table></figure>

<p><img src="https://github.com/yangchao0033/blog/blob/master/ios/2016/1/image/ex9.jpg?raw=true" alt="文字结构"></p>
<p>最后 Draw 一下</p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">-(<span class="keyword">void</span>)characterAttribute</span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">NSString</span> *str = <span class="string">@"This is a test of characterAttribute. 中文字符"</span>;</span><br><span class="line">    <span class="built_in">NSMutableAttributedString</span> *mabstring = [[<span class="built_in">NSMutableAttributedString</span> alloc]initWithString:str];</span><br><span class="line">    </span><br><span class="line">    [mabstring beginEditing];</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     long number = 1;</span></span><br><span class="line"><span class="comment">     CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&amp;number);</span></span><br><span class="line"><span class="comment">     [mabstring addAttribute:(id)kCTCharacterShapeAttributeName value:(id)num range:NSMakeRange(0, 4)];</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     //设置字体属性</span></span><br><span class="line"><span class="comment">     CTFontRef font = CTFontCreateWithName(CFSTR("Georgia"), 40, NULL);</span></span><br><span class="line"><span class="comment">     [mabstring addAttribute:(id)kCTFontAttributeName value:(id)font range:NSMakeRange(0, 4)];</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     //设置字体简隔 eg:test</span></span><br><span class="line"><span class="comment">     long number = 10;</span></span><br><span class="line"><span class="comment">     CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&amp;number);</span></span><br><span class="line"><span class="comment">     [mabstring addAttribute:(id)kCTKernAttributeName value:(id)num range:NSMakeRange(10, 4)];</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     long number = 1;</span></span><br><span class="line"><span class="comment">     CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&amp;number);</span></span><br><span class="line"><span class="comment">     [mabstring addAttribute:(id)kCTLigatureAttributeName value:(id)num range:NSMakeRange(0, [str length])];</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     //设置字体颜色</span></span><br><span class="line"><span class="comment">     [mabstring addAttribute:(id)kCTForegroundColorAttributeName value:(id)[UIColor redColor].CGColor range:NSMakeRange(0, 9)];</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     //设置字体颜色为前影色</span></span><br><span class="line"><span class="comment">     CFBooleanRef flag = kCFBooleanTrue;</span></span><br><span class="line"><span class="comment">     [mabstring addAttribute:(id)kCTForegroundColorFromContextAttributeName value:(id)flag range:NSMakeRange(5, 10)];</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     //设置空心字</span></span><br><span class="line"><span class="comment">     long number = 2;</span></span><br><span class="line"><span class="comment">     CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&amp;number);</span></span><br><span class="line"><span class="comment">     [mabstring addAttribute:(id)kCTStrokeWidthAttributeName value:(id)num range:NSMakeRange(0, [str length])];</span></span><br><span class="line"><span class="comment">     </span></span><br><span class="line"><span class="comment">     //设置空心字颜色</span></span><br><span class="line"><span class="comment">     [mabstring addAttribute:(id)kCTStrokeColorAttributeName value:(id)[UIColor greenColor].CGColor range:NSMakeRange(0, [str length])];</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     long number = 1;</span></span><br><span class="line"><span class="comment">     CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&amp;number);</span></span><br><span class="line"><span class="comment">     [mabstring addAttribute:(id)kCTSuperscriptAttributeName value:(id)num range:NSMakeRange(3, 1)];</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     //设置斜体字</span></span><br><span class="line"><span class="comment">     CTFontRef font = CTFontCreateWithName((CFStringRef)[UIFont italicSystemFontOfSize:20].fontName, 14, NULL);</span></span><br><span class="line"><span class="comment">     [mabstring addAttribute:(id)kCTFontAttributeName value:(id)font range:NSMakeRange(0, 4)];</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     //下划线</span></span><br><span class="line"><span class="comment">     [mabstring addAttribute:(id)kCTUnderlineStyleAttributeName value:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble] range:NSMakeRange(0, 4)];</span></span><br><span class="line"><span class="comment">     //下划线颜色</span></span><br><span class="line"><span class="comment">     [mabstring addAttribute:(id)kCTUnderlineColorAttributeName value:(id)[UIColor redColor].CGColor range:NSMakeRange(0, 4)];</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">    <span class="comment">//对同一段字体进行多属性设置</span></span><br><span class="line">    <span class="comment">//红色</span></span><br><span class="line">    <span class="built_in">NSMutableDictionary</span> *attributes = [<span class="built_in">NSMutableDictionary</span> dictionaryWithObject:(<span class="keyword">id</span>)[<span class="built_in">UIColor</span> redColor].CGColor forKey:(<span class="keyword">id</span>)kCTForegroundColorAttributeName];</span><br><span class="line">    <span class="comment">//斜体</span></span><br><span class="line">    <span class="built_in">CTFontRef</span> font = <span class="built_in">CTFontCreateWithName</span>((<span class="built_in">CFStringRef</span>)[<span class="built_in">UIFont</span> italicSystemFontOfSize:<span class="number">20</span>].fontName, <span class="number">40</span>, <span class="literal">NULL</span>);</span><br><span class="line">    [attributes setObject:(__bridge <span class="keyword">id</span>)font forKey:(<span class="keyword">id</span>)kCTFontAttributeName];</span><br><span class="line">    <span class="comment">//下划线</span></span><br><span class="line">    [attributes setObject:(<span class="keyword">id</span>)[<span class="built_in">NSNumber</span> numberWithInt:kCTUnderlineStyleDouble] forKey:(<span class="keyword">id</span>)kCTUnderlineStyleAttributeName];</span><br><span class="line">    </span><br><span class="line">    [mabstring addAttributes:attributes range:<span class="built_in">NSMakeRange</span>(<span class="number">0</span>, <span class="number">4</span>)];</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">    <span class="built_in">NSRange</span> kk = <span class="built_in">NSMakeRange</span>(<span class="number">0</span>, <span class="number">4</span>);</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">NSDictionary</span> * dc = [mabstring attributesAtIndex:<span class="number">0</span> effectiveRange:&amp;kk];</span><br><span class="line">    </span><br><span class="line">    [mabstring endEditing];</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">NSLog</span>(<span class="string">@"value = %@"</span>,dc);</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">    <span class="built_in">CTFramesetterRef</span> framesetter = <span class="built_in">CTFramesetterCreateWithAttributedString</span>((<span class="built_in">CFAttributedStringRef</span>)mabstring);</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">CGMutablePathRef</span> Path = <span class="built_in">CGPathCreateMutable</span>();</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">CGPathAddRect</span>(Path, <span class="literal">NULL</span> ,<span class="built_in">CGRectMake</span>(<span class="number">10</span> , <span class="number">0</span> ,<span class="keyword">self</span>.bounds.size.width<span class="number">-10</span> , <span class="keyword">self</span>.bounds.size.height<span class="number">-10</span>));</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">CTFrameRef</span> frame = <span class="built_in">CTFramesetterCreateFrame</span>(framesetter, <span class="built_in">CFRangeMake</span>(<span class="number">0</span>, <span class="number">0</span>), Path, <span class="literal">NULL</span>);</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//获取当前(View)上下文以便于之后的绘画，这个是一个离屏。</span></span><br><span class="line">    <span class="built_in">CGContextRef</span> context = <span class="built_in">UIGraphicsGetCurrentContext</span>();</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">CGContextSetTextMatrix</span>(context , <span class="built_in">CGAffineTransformIdentity</span>);</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//压栈，压入图形状态栈中.每个图形上下文维护一个图形状态栈，并不是所有的当前绘画环境的图形状态的元素都被保存。图形状态中不考虑当前路径，所以不保存</span></span><br><span class="line">    <span class="comment">//保存现在得上下文图形状态。不管后续对context上绘制什么都不会影响真正得屏幕。</span></span><br><span class="line">    <span class="built_in">CGContextSaveGState</span>(context);</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//x，y轴方向移动</span></span><br><span class="line">    <span class="built_in">CGContextTranslateCTM</span>(context , <span class="number">0</span> ,<span class="keyword">self</span>.bounds.size.height);</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//缩放x，y轴方向缩放，－1.0为反向1.0倍,坐标系转换,沿x轴翻转180度</span></span><br><span class="line">    <span class="built_in">CGContextScaleCTM</span>(context, <span class="number">1.0</span> ,<span class="number">-1.0</span>);</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">CTFrameDraw</span>(frame,context);</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">CGPathRelease</span>(Path);</span><br><span class="line">    <span class="built_in">CFRelease</span>(framesetter);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (<span class="keyword">void</span>)drawRect:(<span class="built_in">CGRect</span>)rect</span><br><span class="line">&#123;</span><br><span class="line">    [<span class="keyword">self</span> characterAttribute];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>本文博客地址：<a href="http://superyang.gitcafe.io/blog/2016/01/26/coretextji-chu/" target="_blank" rel="noopener">http://superyang.gitcafe.io/blog/2016/01/26/coretextji-chu/</a></p>

    </div>

    
    
    
        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="https://www.jianshu.com/u/f37a8f0ba6f8">
            <span class="icon">
              <i class="fas fa-book"></i>
            </span>

            <span class="label">简书</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/RAC/" rel="tag"># RAC</a>
              <a href="/tags/FRP/" rel="tag"># FRP</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2016/01/11/%E4%B8%BAHarpy%EF%BC%88ios%E7%89%88%E6%9C%AC%E6%9B%B4%E6%96%B0%E5%B7%A5%E5%85%B7)%E5%88%B6%E5%81%9A%E5%85%BC%E5%AE%B9%E7%89%88%E6%9C%AC/" rel="prev" title="为Harpy（ios版本更新工具)制做兼容版本">
      <i class="fa fa-chevron-left"></i> 为Harpy（ios版本更新工具)制做兼容版本
    </a></div>
      <div class="post-nav-item">
    <a href="/2016/01/29/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8iOS%E5%AE%9E%E7%8E%B0%E3%80%8A%E8%8A%88%E6%9C%88%E4%BC%A0%E3%80%8B%E7%9A%84%E7%9B%B4%E6%92%AD%E3%80%81%E7%82%B9%E6%92%AD%E3%80%81%E7%BC%93%E5%AD%98%EF%BC%9F-HTTP-Live-Streaming-(HLS)%EF%BC%88%E4%B8%80%EF%BC%89/" rel="next" title="如何使用iOS实现《芈月传》的直播、点播、缓存？-HTTP-Live-Streaming-(HLS)（一）">
      如何使用iOS实现《芈月传》的直播、点播、缓存？-HTTP-Live-Streaming-(HLS)（一） <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#字体的基本知识"><span class="nav-number">1.</span> <span class="nav-text">字体的基本知识</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="SuperYang"
      src="https://tva2.sinaimg.cn/crop.259.17.290.290.180/c2abdfa9jw8ez7appr3p2j20g40a2t91.jpg?KID=imgbed,tva&Expires=1594186820&ssig=BdxhDdi1Ti">
  <p class="site-author-name" itemprop="name">SuperYang</p>
  <div class="site-description" itemprop="description">牛肉炒饭不要香菜，老板收钱</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">39</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">5</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">31</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/yangchao0033" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;yangchao0033" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:ygrfwyc@gmail.com" title="E-Mail → mailto:ygrfwyc@gmail.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://weibo.com/yangchao0033" title="Weibo → https:&#x2F;&#x2F;weibo.com&#x2F;yangchao0033" rel="noopener" target="_blank"><i class="fab fa-weibo fa-fw"></i>Weibo</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.jianshu.com/u/f37a8f0ba6f8" title="简书 → https:&#x2F;&#x2F;www.jianshu.com&#x2F;u&#x2F;f37a8f0ba6f8" rel="noopener" target="_blank"><i class="fas fa-book fa-fw"></i>简书</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://t.me/superYang0033" title="Telegram → https:&#x2F;&#x2F;t.me&#x2F;superYang0033" rel="noopener" target="_blank"><i class="fab fa-telegram fa-fw"></i>Telegram</a>
      </span>
  </div>



      </div>

      
        <div id="music163player">
        <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=350 height=86 src="//music.163.com/outchain/player?type=2&id=31473269&auto=0&height=66">
        </iframe>
        </div>
    </div>

    

  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">SuperYang</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">157k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">2:23</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  
  <script>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>













  

  

  

  <script async src="/js/cursor/fireworks.js"></script>



<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : true,
      notify     : true,
      appId      : 'zizRqalUJY55Xc5oBJKbhxpV-gzGzoHsz',
      appKey     : '64gPcVi8lIA8zVGpBSEk7uKu',
      placeholder: "留言板无需注册登录，快来评论吧。。",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : true,
      lang       : '' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
